package com.aos.iot.protocol.hll.log.builder;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.IdUtil;
import com.aos.iot.protocol.hll.log.MessageLog;
import com.aos.iot.socket.common.message.Message;
import com.aos.iot.protocol.hll.util.CodeUtil;

public interface ILogBuilder {

    MessageLog upBuild(Message message, Long snowflakeNextId);

    MessageLog downBuild(Message message, Long snowflakeNextId);

    Class<? extends Message> messageClass();

    default ILogBuilder logBuilder() {
        return this;
    }

    default MessageLog build(Message message) {
        Long snowflakeNextId = IdUtil.getSnowflakeNextId();
        if(message.getDirection() == Message.Direction.UP) {
            return upBuild(message, snowflakeNextId);
        } else {
            return downBuild(message, snowflakeNextId);
        }
    }

    default String getPropValueStr(Dict data, String key, boolean isHigh, String defaultValue) {
        return isHigh ? CodeUtil.highRotation(data.getStr(key)) : data.get(key, defaultValue);
    }
}
